package com.runfast.waimai.service.impl;

import com.runfast.common.exception.BaseException;
import com.runfast.common.service.BaseService;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.waimai.dao.mapper.RunfastPictureMapper;
import com.runfast.waimai.dao.mapper.RunfastPictureclassMapper;
import com.runfast.waimai.dao.model.RunfastPictureExample;
import com.runfast.waimai.dao.model.RunfastPictureclass;
import com.runfast.waimai.dao.model.RunfastPictureclassExample;
import com.runfast.waimai.service.RunfastPictureclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RunfastPictureclassServiceImpl extends BaseService<RunfastPictureclass, Integer, RunfastPictureclassExample> implements RunfastPictureclassService {
    @Autowired
    private RunfastPictureclassMapper runfastPictureclassMapper;
    @Autowired
    private RunfastPictureMapper runfastPictureMapper;

    @Override
    public void updateDeleteStatus(RunfastPictureclass pictureClass) {
        //判断此类型是否在使用
        RunfastPictureExample pictureExample = new RunfastPictureExample();
        pictureExample.createCriteria().andTypeidEqualTo(pictureClass.getId()).andIsdeleteEqualTo(1);
        Integer total = runfastPictureMapper.countByExample(pictureExample);
        if(total>0) {
            throw new BaseException(ResultCode.FAIL,"此分类或子分类正在被使用，不能删除");
        }else {
            pictureClass.setCreatetime(new Date());
            pictureClass.setIsdelete(0);
            runfastPictureclassMapper.updateByPrimaryKeySelective(pictureClass);//改变需要删除分类的删除状态
        }
    }
}